package com.fpt.quanlyhethongsan.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.fpt.quanlyhethongsan.utils.Constants;
import com.fpt.quanlysanbong.dao.PersonDao;
import com.fpt.quanlysanbong.dao.SoccerFieldDao;
import com.fpt.quanlysanbong.domain.Account;
import com.fpt.quanlysanbong.domain.SoccerField;

public class UserController {
	private int id;
	private String operation;
	private int roleId;
	private String name;
	private String email;
	private String phone;
	private String address;
	private String username;
	private String password;
	private String soccerFieldId;
	private Account acc;
	private boolean isStaff = false;
	private List<SoccerField> listSoccerField;
	private List<Account> staffLst;

	public List<Account> getStaffLst() {
		return staffLst;
	}

	public void setStaffLst(List<Account> staffLst) {
		this.staffLst = staffLst;
	}

	public List<SoccerField> getListSoccerField() {
		return listSoccerField;
	}

	public void setListSoccerField(List<SoccerField> listSoccerField) {
		this.listSoccerField = listSoccerField;
	}

	// get thong tin nguoi dung
	public String getUserProfile() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;

		acc = (Account) session.getAttribute(Constants.USER_SESSION);

		setOperation(Constants.UPDATE);
		return Constants.SUCCESS;
	}

	// get danh sach nhan vien
	public String getListStaff() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;

		acc = (Account) session.getAttribute(Constants.USER_SESSION);

		listSoccerField = new SoccerFieldDao().getListSoccerField(acc.getPerson().getId());

		staffLst = new ArrayList<Account>();

		if (CollectionUtils.isNotEmpty(listSoccerField)) {
			for (SoccerField sc : listSoccerField) {
				if (sc.getStaff() != null) {
					Account staff = new PersonDao().getAccountByPersonId(sc.getStaff().getId());
					staffLst.add(staff);
				}
			}
		}

		setAcc(null);
		setOperation(Constants.UPDATE);
		isStaff = true;

		return Constants.SUCCESS;
	}

	public String getSoccerFieldName(int staffId) {
		SoccerField sc = new SoccerFieldDao().getSoccerFieldByStaff(staffId);
		return sc.getName();
	}

	public int getSoccerFieldId(int staffId) {
		SoccerField sc = new SoccerFieldDao().getSoccerFieldByStaff(staffId);
		return sc.getId();
	}

	public String registerUser() {
		setOperation(Constants.ADD);
		return Constants.SUCCESS;
	}

	public String registerStaff() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;

		acc = (Account) session.getAttribute(Constants.USER_SESSION);
		listSoccerField = new SoccerFieldDao().getListSoccerField(acc.getPerson().getId());

		setAcc(null);
		isStaff = true;
		setOperation(Constants.ADD);
		return Constants.SUCCESS;
	}

	public String changePassword() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;

		acc = (Account) session.getAttribute(Constants.USER_SESSION);

		setOperation(Constants.CHANGE_PASSWORD);
		return Constants.SUCCESS;
	}

	public String saveAccount() {
		PersonDao personDao = new PersonDao();
		boolean isSuccess = false;
		acc = new Account();

		acc.getPerson().setName(name);
		acc.getPerson().setEmail(email);
		acc.getPerson().setAddress(address);
		acc.getPerson().setPhone(phone);
		acc.getRole().setId(roleId);

		if (StringUtils.isNotBlank(soccerFieldId)) {
			acc.getPerson().setSoccerFieldId(Integer.parseInt(soccerFieldId));
			isStaff = true;
		}

		if (StringUtils.equalsIgnoreCase(operation, Constants.UPDATE)) {
			acc.setId(id);
			isSuccess = personDao.updateAccount(acc);
		} else if (StringUtils.equalsIgnoreCase(operation, Constants.ADD)) {
			acc.setUsername(username);
			acc.setPassword(password);
			isSuccess = personDao.addNewAccount(acc);
		} else if (StringUtils.equalsIgnoreCase(operation, Constants.CHANGE_PASSWORD)) {
			acc.setId(id);
			acc.setPassword(password);
			isSuccess = (personDao.changePassword(acc));
		}

		HttpSession session = ServletActionContext.getRequest().getSession();
		Account account = (Account) session.getAttribute(Constants.USER_SESSION);

		if (account != null && acc.getId() == account.getId()) {
			acc = personDao.getAccountById(account.getId());
			session.setAttribute(Constants.USER_SESSION, acc);
		}

		// close connection;
		personDao.closeConnection();
		
		if (isStaff) {
			try {
				ServletActionContext.getResponse().sendRedirect("listStaff");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (isSuccess) {
			return Constants.SUCCESS;
		} else {
			return Constants.ERROR;
		}
	}

	public String getMailInbox() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		listSoccerField = new SoccerFieldDao().getListSoccerField();
		return Constants.SUCCESS;
	}

	public String acceptSoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		boolean isSuccess = new SoccerFieldDao().acceptSoccerField(id);

		if (isSuccess) {
			return Constants.SUCCESS;
		} else {
			return Constants.ERROR;
		}
	}

	public String denySoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		boolean isSuccess = new SoccerFieldDao().denySoccerField(id);
		if (isSuccess) {
			return Constants.SUCCESS;
		} else {
			return Constants.ERROR;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSoccerFieldId() {
		return soccerFieldId;
	}

	public void setSoccerFieldId(String soccerFieldId) {
		this.soccerFieldId = soccerFieldId;
	}

	public Account getAcc() {
		return acc;
	}

	public void setAcc(Account acc) {
		this.acc = acc;
	}

	public boolean isStaff() {
		return isStaff;
	}

	public void setStaff(boolean isStaff) {
		this.isStaff = isStaff;
	}

}
